rm(list = ls())


library(foreign)  #for reading SPSS file

#setwd("")

RUS_FIRMS<- read.spss("h_RUS_FIRMS_2010_RAWDATA.sav",use.value.labels=FALSE)

attach(RUS_FIRMS)



city<-CITY
city[city!=1&city!=2]<-"regions"
city[city==1]<-"Moscow"
city[city==2]<-"St.Pete"

city2<-CITY
city2[city2==1]<-"Moscow"
city2[city2==2]<-"St.Pete"
city2[city2==3]<-"Ekaterinburg"
city2[city2==4]<-"Novosibirsk"
city2[city2==5]<-"Rostov.on.Don"
city2[city2==6]<-"Kazan"
city2[city2==7]<-"Nizhniy.Novgorod"
city2[city2==8]<-"Samara"

##########


Prts_viol1<-B4
Prts_viol1[Prts_viol1==9]<-NA

Srts_viol1<-B5
Srts_viol1[Srts_viol1==9]<-NA

Prts_viol2<-Prts_viol1
Prts_viol2=ifelse(Prts_viol1==1,0,1)

Srts_viol2<-Srts_viol1
Srts_viol2=ifelse(Srts_viol1==1,0,1)

rts_viol<-Prts_viol2
rts_viol[Srts_viol2==1]<-1

###############

sued<-B8
sued[sued==9]<-NA

sued2<-sued
sued2=ifelse(sued==1,0,1)

defendant<-B9
defendant[defendant==9]<-NA

defendant2<-defendant
defendant2=ifelse(defendant==1,0,1)

litigated<-sued2
litigated[defendant2==1]<-1

#########################

willing_to_use<-B10
willing_to_use[willing_to_use==9]<-NA

violence<-B13A
violence[violence ==9]<-NA

violence2=ifelse(violence>2,1,0)

other_firms<-B19a
other_firms[other_firms==9]<-NA

other_firms2=ifelse(other_firms>3,1,0)

other_firms2_r<-1-other_firms2

other_past<-B19b
other_past[other_past==9]<-NA

other_past2=ifelse(other_past>3,1,0)

contact.mafia<-B20
contact.mafia[contact.mafia==2]<-0
contact.mafia[contact.mafia==3]<-NA

recent.contact.mafia<-contact.mafia
recent.contact.mafia[B20A==2]<-0
recent.contact.mafia[B20A==3]<-NA


############

negotiate<-EAa
negotiate[negotiate==9]<-NA
lawenf<-EAb
lawenf[lawenf==9]<-NA
lawenf_inf<-EAc
lawenf_inf[lawenf_inf==9]<-NA
bur<-EAd
bur[bur==9]<-NA
bur_inf<-EAe
bur_inf[bur_inf==9]<-NA
lawyers<-EAf
lawyers[lawyers==9]<-NA
courts<-EAg
courts[courts==9]<-NA
courts_inf<-EAh
courts_inf[courts_inf==9]<-NA
arbitrate<-EAi
arbitrate[arbitrate==9]<-NA
mafia<-EAj
mafia[mafia==9]<-NA
psa<-EAk
psa[psa==9]<-NA
pss<-EAl
pss[pss==9]<-NA
bus_assoc<-EAm
bus_assoc[bus_assoc==9]<-NA

negotiate2<-EBa
negotiate2[negotiate2==9]<-NA
lawenf2<-EBb
lawenf2[lawenf2==9]<-NA
lawenf_inf2<-EBc
lawenf_inf2[lawenf_inf2==9]<-NA
bur2<-EBd
bur2[bur2==9]<-NA
bur_inf2<-EBe
bur_inf2[bur_inf2==9]<-NA
lawyers2<-EBf
lawyers2[lawyers2==9]<-NA
courts2<-EBg
courts2[courts2==9]<-NA
courts_inf2<-EBh
courts_inf2[courts_inf2==9]<-NA
arbitrate2<-EBi
arbitrate2[arbitrate2==9]<-NA
mafia2<-EBj
mafia2[mafia2==9]<-NA
psa2<-EBk
psa2[psa2==9]<-NA
pss2<-EBl
pss2[pss2==9]<-NA
bus_assoc2<-EBm
bus_assoc2[bus_assoc2==9]<-NA




#######


use_court2<-B25A_1
use_court2[use_court2!=10&use_court2!=98]<-1
use_court2[use_court2==10|B26A_1==10]<-0
use_court2[use_court2==98|B26A_1==98]<-NA


use_bur2<-B25E_1
use_bur2[use_bur2!=10&use_bur2!=98]<-1
use_bur2[use_bur2==10|B26E_1==10]<-0
use_bur2[use_bur2==98|B26E_1==98]<-NA

use_lawenf2<-B25L_1
use_lawenf2[use_lawenf2!=10&use_lawenf2!=98]<-1
use_lawenf2[use_lawenf2==10|B26L_1==10]<-0
use_lawenf2[use_lawenf2==98|B26L_1==98]<-NA

use_court_inf2<-B25B_1
use_court_inf2[use_court_inf2!=10&use_court_inf2!=98]<-1
use_court_inf2[use_court_inf2==10|B26B_1==10]<-0
use_court_inf2[use_court_inf2==98|B26B_1==98]<-NA

use_bur_inf2<-B25F_1
use_bur_inf2[use_bur_inf2!=10&use_bur_inf2!=98]<-1
use_bur_inf2[use_bur_inf2==10|B26F_1==10]<-0
use_bur_inf2[use_bur_inf2==98|B26F_1==98]<-NA

use_lawenf_inf2<-B25M_1
use_lawenf_inf2[use_lawenf_inf2!=10&use_lawenf_inf2!=98]<-1
use_lawenf_inf2[use_lawenf_inf2==10|B26M_1==10]<-0
use_lawenf_inf2[use_lawenf_inf2==98|B26M_1==98]<-NA

use_mafia2<-B25J_1
use_mafia2[use_mafia2!=10&use_mafia2!=98]<-1
use_mafia2[use_mafia2==10|B26J_1==10]<-0
use_mafia2[use_mafia2==98|B26J_1==98]<-NA

use_psa2<-B25C_1
use_psa2[use_psa2!=10& use_psa2!=98]<-1
use_psa2[use_psa2 ==10|B26C_1==10]<-0
use_psa2[use_psa2 ==98|B26C_1==98]<-NA

use_pss2<-B25D_1
use_pss2[use_pss2!=10& use_pss2!=98]<-1
use_pss2[use_pss2 ==10|B26D_1==10]<-0
use_pss2[use_pss2 ==98|B26D_1==98]<-NA

use_bus_assoc2<-B25G_1
use_bus_assoc2[use_bus_assoc2!=10& use_bus_assoc2!=98]<-1
use_bus_assoc2[use_bus_assoc2 ==10|B26G_1==10]<-0
use_bus_assoc2[use_bus_assoc2 ==98|B26G_1==98]<-NA

use_arbitrate2<-B25K_1
use_arbitrate2[use_arbitrate2!=10& use_arbitrate2!=98]<-1
use_arbitrate2[use_arbitrate2 ==10|B26K_1==10]<-0
use_arbitrate2[use_arbitrate2 ==98|B26K_1==98|B26K_1==97]<-NA

######

BA<-B27
BA[BA==2]<-0

############

size<-B30

size_dummies<-size
size_dummies[size<101]<-1
size_dummies[size>100&size<251]<-2
size_dummies[size>250&size<501]<-3
size_dummies[size>500]<-4

#######

sector<-B31
sector[sector==1]<-"product"
sector[sector==2]<-"service"
sector[sector==3]<-"z_IP"

sect<-sector
sect[sect=="z_IP"]<-"service"

sector2<-B32
sector2[sector2>10]<-11
sector2<-as.factor(sector2)

sector3<-sector2
sector3[sector3==4]<-11

############

privatized<-B33
privatized[privatized==1]<-0
privatized[privatized==3|privatized==4]<-1
privatized[privatized!=1]<-0

firm_age<-B34
firm_age[firm_age>2008]<-1
firm_age[firm_age>2006 & firm_age <2009]<-2
firm_age[firm_age>2004 & firm_age <2007]<-3
firm_age[firm_age>1999 & firm_age <2005]<-4
firm_age[firm_age>1994 & firm_age <2000]<-5
firm_age[firm_age>1980 & firm_age<1995]<-6
firm_age[is.na(firm_age)==TRUE]<-B34CODE[is.na(B34CODE)==FALSE]
firm_age[firm_age==9]<-NA

gov_own<-B35B
gov_own[gov_own==2|gov_own==3]<-0
gov_own[gov_own==9]<-NA

foreign_own<-B35C
foreign_own[foreign_own==2|foreign_own==3]<-0
foreign_own[foreign_own==9]<-NA

growth<-B48
growth[growth==9]<-NA

###############

cash<-B50
cash[cash ==9]<-NA

cash_dich<-cash
cash_dich =ifelse(cash==1,1,0)


######


tax1<-B51
tax1[tax1==9]<-NA

tax1_dich<-tax1
tax1_dich=ifelse(tax1<6,0,1)
tax1_dich_r<-1-tax1_dich


######
job_descript<-as.factor(B55.1)
job_descript[job_descript==6|job_descript==7|job_descript==8|job_descript==12|job_descript==13|job_descript==14|job_descript==15|job_descript==16|job_descript==17|job_descript==18|job_descript==19]<-5

age<-B56

ed<-as.factor(B57)

leg_ed<-B57
leg_ed[leg_ed!=6&leg_ed!=7]<-0
leg_ed[leg_ed!=0]<-1

gender<-B59
gender[gender==2]<-0


#######

detach(RUS_FIRMS)

RUS_FIRMS_CLEANED<-data.frame(city, city2, Prts_viol1, Srts_viol1, Prts_viol2, Srts_viol2, rts_viol, sued,  defendant, sued2, defendant2, litigated, violence, violence2, willing_to_use,other_firms, other_firms2, other_firms2_r,other_past, other_past2, contact.mafia, recent.contact.mafia,   negotiate2, lawenf2, lawenf_inf2, bur2, bur_inf2, lawyers2,courts2, courts_inf2, arbitrate2, mafia2, psa2, pss2, bus_assoc2,negotiate, lawenf, lawenf_inf, bur, bur_inf, lawyers,courts, courts_inf, arbitrate, mafia, psa, pss, bus_assoc, use_court2, use_court_inf2, use_bur2, use_bur_inf2, use_lawenf2, use_lawenf_inf2, use_arbitrate2, use_psa2, use_pss2, use_bus_assoc2, use_mafia2,   BA, size, size_dummies, sector, sect, sector2, sector3, firm_age, privatized,gov_own, foreign_own, growth, cash, cash_dich, tax1,  tax1_dich, tax1_dich_r, job_descript, age, gender, leg_ed)

save(RUS_FIRMS_CLEANED,file="RUS_FIRMS_ORIG.RData")
#click on file to load
#assign("RUS_FIRMS_ORIG",RUS_FIRMS_CLEANED)
#save(RUS_FIRMS_ORIG,file="RUS_FIRMS_ORIG.RData")
#write.table(RUS_FIRMS_CLEANED,file="RUS_FIRMS_ORIG.csv",sep=",",row.names=F)
#library(foreign)
write.dta(RUS_FIRMS_CLEANED,file="RUS_FIRMS_ORIG.dta")


